package com.example.tao_city.spinnertext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by TAO_CITY on 2018/2/5.
 */

public class GridViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private GridView gridView;
    private int[] img = {R.drawable.home_black, R.drawable.home_blue, R.drawable.icon_weixin
            , R.drawable.icon_weixin1, R.drawable.icon_wode, R.drawable.icon_yinglian1, R.drawable.icon_zhifubao
            , R.drawable.icon_zhifubao1, R.drawable.life_black, R.drawable.life_blue, R.drawable.more_black
            , R.drawable.more_blue, R.drawable.my_back, R.drawable.my_blue};
    private List<Map<String, Object>> datalist = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview);
        gridView = findViewById(R.id.gridview);
        int[] to = {R.id.imageview};
        String[] form = new String[]{"img"};
        SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.griditen, form, to);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);

    }

    private List<Map<String, Object>> getData() {
        for (int i = 0; i < img.length; i++) {
            Map map = new HashMap();
            map.put("img", img[i]);
            datalist.add(map);
        }
        return datalist;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "点击了" + position, Toast.LENGTH_SHORT).show();
    }
}
